var jPusher = {
    onTagsWithAlias: function(event) {
        try {
            console.log("onTagsWithAlias");
            var result = "result code:" + event.resultCode + " ";
            result += "tags:" + event.tags + " ";
            result += "alias:" + event.alias + " ";
            console.log('tag结果：' + result);
        } catch (exception) {
            console.log(exception);
        }
    },
    onOpenNotification: function(event) {
        try {
            var alertContent, alertType, alertId;
            if (device.platform == "Android") {
                alertContent = event.alert;
                alertType = event.extras.msgType;
                alertId = alertType == 1 ? event.extras.taskId : event.extras.msgId;
            } else {
                alertContent = event.aps.alert;
                alertType = event.aps.extras.msgType;
                alertId = alertType == 1 ? event.aps.extras.taskId : event.aps.extras.msgId;
            }
            console.log("打开通知:" + alertContent);

            if (alertType == 1) {
                $.openWindow({
                    url: 'missionDetail.html?id=' + alertId
                });
            } else {
                $.openWindow({
                    url: 'messageDetail.html?id=' + alertId + '&isread=0&isdirect=1'
                });
            }

            $.layer('close', window.recevieMsgAlert);

        } catch (exception) {
            console.log("打开通知异常" + exception);
        }
    },
    onReceiveMessage: function(event) {
        try {
            var message;
            if (device.platform == "Android") {
                message = event.message;
            } else {
                message = event.content;
            }
            console.log("接受到的消息" + message);
        } catch (exception) {
            console.log("接受消息异常-->" + exception);
        }
    },
    onReceiveNotification: function(event) {
        try {
            var alertContent, alertType, alertId, alertTitle, alertSender;
            if (device.platform == "Android") {
                alertSender = event.title;
                alertContent = event.alert;
                alertType = event.extras.msgType;
                alertId = alertType == 1 ? event.extras.taskId : event.extras.msgId;
            } else {
                alertSender = event.aps.title;
                alertContent = event.aps.alert;
                alertType = event.aps.extras.msgType;
                alertId = alertType == 1 ? event.aps.extras.taskId : event.aps.extras.msgId;
            }
            console.log('接受到的通知：' + alertContent);

            alertTitle = alertType == 1 ? '收到新的任务' : '收到新的新消息';


            var contentHtml = ['<div m-role="cell" m-align="left" class="color-blue margin-b-10">来自于：', alertSender, '</div>', '<div m-role="cell" m-align="left">', alertContent, '</div>'].join('');
            window.recevieMsgAlert = $.layer({
                type: 'confirm',
                title: alertTitle,
                width: '80%',
                content: contentHtml,
                shade: false,
                btn: ['关闭', '查看'],
                ok: function(idx) {
                    if (alertType == 1) {
                        $.openWindow({
                            url: 'missionDetail.html?id=' + alertId + '&isdirect=1'
                        });
                        homeAction.refresh();
                        missionAction.refresh();
                    } else if (alertType === 0) {
                        $.openWindow({
                            url: 'messageDetail.html?id=' + alertId + '&isread=0&isdirect=1'
                        });
                    } else if (alertType == 2) {
                        $.openWindow({
                            url: 'messageDetail.html?id=' + alertId + '&isread=0&isdirect=1'
                        });
                        if ($("#missionView").attr('isinit') == '1') missionAction.refresh();
                        shipAction.refresh();
                    }

                    window.plugins.jPushPlugin.clearAllNotification();
                    $.layer('close', idx);
                },
                cancel: function(idx) {
                    homeAction.refresh();
                    if (alertType == 2) {
                        if ($("#missionView").attr('isinit') == '1') missionAction.refresh();
                        shipAction.refresh();
                    }
                    if (alertType == 1 && $("#missionView").attr('isinit') == '1') missionAction.refresh();
                    window.plugins.jPushPlugin.clearAllNotification();
                    $.layer('close', idx);
                }
            });
        } catch (exception) {
            console.log("接受消息异常" + exception);
        }
    },
    init: function() {
        console.log('开始了~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
        document.addEventListener("jpush.setTagsWithAlias", jPusher.onTagsWithAlias, false);
        document.addEventListener("jpush.openNotification", jPusher.onOpenNotification, false);
        document.addEventListener("jpush.receiveNotification", jPusher.onReceiveNotification, false);
        document.addEventListener("jpush.receiveMessage", jPusher.onReceiveMessage, false);


        var onGetRegistrationID = function(data) {
            try {
                console.log("JPushPlugin:registrationID is " + data);

                if (data.length === 0) {
                    var t1 = window.setTimeout(getRegistrationID, 1000);
                } else {
                    if (!data) $.toast("jPush ID 为空，请联系管理员");
                    window.RegistrationID = data;
                    loginAction.init();

                }
            } catch (exception) {
                console.log(exception);
            }
        };

        var getRegistrationID = function() {
            window.plugins.jPushPlugin.getRegistrationID(onGetRegistrationID);
        };

        try {
            window.plugins.jPushPlugin.init();
            getRegistrationID();


            if (device.platform != "Android") {
                window.plugins.jPushPlugin.setDebugModeFromIos();
                window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0);
            } else {
                window.plugins.jPushPlugin.setDebugMode(true);
                window.plugins.jPushPlugin.setStatisticsOpen(true);
            }
        } catch (exception) {
            console.log(exception);
        }
    }
};

module.exports = jPusher;
